package cn.edu.cug.cs.gtl.oi.ui;

import cn.edu.cug.cs.gtl.api.wrapper.TableWrapper;
import cn.edu.cug.cs.gtl.protos.Layer;
import cn.edu.cug.cs.gtl.protos.Table;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;
import java.awt.*;

public class TablePanel extends JPanel {

    MainApp theApp = null;
    JTable  tableComponent = null;

    public TablePanel(MainApp theApp,LayoutManager layout, boolean isDoubleBuffered) {
        super(layout, isDoubleBuffered);
        this.theApp=theApp;
    }

    public TablePanel(MainApp theApp,LayoutManager layout) {
        super(layout);
        this.theApp=theApp;
    }

    public TablePanel(MainApp theApp,boolean isDoubleBuffered) {
        super(isDoubleBuffered);
        this.theApp=theApp;
    }

    public TablePanel(MainApp theApp) {
        this.theApp=theApp;
    }

    public JTable getTableComponent() {
        return tableComponent;
    }

    public void initial(){
        Layer curLayer = theApp.getCurrentLayer();
        Table table =null;
        if(curLayer != null ) table=theApp.getCurrentLayer().getAttributeTable();

        if(table==null){
            tableComponent = new JTable();
            setLayout(new BorderLayout());
            add(tableComponent,BorderLayout.CENTER);
        }
        else{
            TableWrapper wrapper = new TableWrapper(table);
            tableComponent = new JTable(wrapper.getData(),wrapper.getColumnNames());

            setLayout(new BorderLayout());
            add(new JScrollPane(tableComponent),BorderLayout.CENTER);
        }
    }

    public void reset(){
        Layer curLayer = theApp.getCurrentLayer();
        Table table =null;
        if(curLayer != null )
            table=theApp.getCurrentLayer().getAttributeTable();

        tableComponent.removeAll();

        if(table==null){

        }
        else{
            TableWrapper wrapper = new TableWrapper(table);

            tableComponent.setModel(new DefaultTableModel(wrapper.getData(),wrapper.getColumnNames()));
        }
    }

}
